package com.wanmait.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wanmait.movie.entity.Cinema;
import com.wanmait.movie.entity.Lobby;
import com.wanmait.movie.entity.LobbyType;
import com.wanmait.movie.entity.VO.LobbyVO;
import com.wanmait.movie.entity.request.manage.lobby.AddLobbyRequest;
import com.wanmait.movie.entity.request.manage.lobby.UpLobbySeatRequest;
import com.wanmait.movie.mapper.LobbyMapper;
import com.wanmait.movie.service.LobbyService;
import com.wanmait.movie.util.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class LobbyServiceImpl extends ServiceImpl<LobbyMapper, Lobby> implements LobbyService {
    @Resource
    private LobbyMapper lobbyMapper;

    @Override
    public List<Lobby> getLobbyListByCinemaId(Integer id) {
        LambdaQueryWrapper<Lobby> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Lobby::getCinemaId, id);
        return lobbyMapper.selectList(queryWrapper);
    }

    @Override
    public int getCinemaIdByLobbyId(Integer lobbyId) {
        return lobbyMapper.selectById(lobbyId).getCinemaId();
    }

    /**
     * 获取影厅信息
     * @return 影厅信息
     */
    @Override
    public List<LobbyVO> getLobbyInfo() {
        MPJLambdaWrapper<Lobby> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Lobby.class)
                .select(LobbyType::getLobbyTypeName)
                .select(Cinema::getCinemaName)
                .leftJoin(LobbyType.class, LobbyType::getId, Lobby::getLobbyTypeId)
                .leftJoin(Cinema.class, Cinema::getId, Lobby::getCinemaId);
        return lobbyMapper.selectJoinList(LobbyVO.class,wrapper);
    }

    /**
     * 添加影厅信息
     * @param lobbyRequest 影厅信息
     * @param request request
     * @return 添加结果
     */
    @Override
    public Integer addLobby(AddLobbyRequest lobbyRequest, HttpServletRequest request) {
        Lobby newLobby = new Lobby();
        BeanUtils.copyProperties(lobbyRequest,newLobby);
        Integer currentAdminId = Integer.parseInt(JWTUtils.getAudience(request.getHeader("token")));
        newLobby.setInsertAdminId(currentAdminId);
        newLobby.setUpdateAdminId(currentAdminId);
        this.save(newLobby);
        return newLobby.getId();
    }

    @Override
    public boolean upLobbySeat(UpLobbySeatRequest upLobbySeatRequest, HttpServletRequest request) {
        Lobby lobby = new Lobby();
        BeanUtils.copyProperties(upLobbySeatRequest,lobby);
        Integer currentAdminId = Integer.parseInt(JWTUtils.getAudience(request.getHeader("token")));
        lobby.setUpdateAdminId(currentAdminId);
        lobby.setUpdateTime(new Date());
        return this.updateById(lobby);
    }
}
